package com.shun.upms.action;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shun.common.R;
import com.shun.common.RC;
import com.shun.common.log.LogType;
import com.shun.common.log.annotation.ShunLog;
import com.shun.upms.common.UpmsConstant;
import com.shun.upms.dto.SysAdminUserDto;
import com.shun.upms.dto.SysAdminUserQueryDto;
import com.shun.upms.service.ISysAdminUserService;
import com.shun.upms.vo.SysAdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 管理人员表 前端控制器
 * </p>
 *
 * @author clever
 * @since 2021-09-27
 */
@Slf4j
@RestController
@RequestMapping("/upms/user")
@Api(tags = "用户的API")
public class SysAdminUserAction {

    @Autowired
    private ISysAdminUserService adminUserService;

    /**
     * 检查用户账号和密码匹配
     * @return
     */
    @PostMapping("/v1/chkAccAndPwd")
    @ApiOperation(value = "账号与密码验证,获取用户的userId")
    public R checkUserPasswd(@RequestParam(required = true)String userAccount, @RequestParam(required = true)String password){
        return adminUserService.getUserIdByAccAndPwd(userAccount,password);
    }

    /**
     * 检查用户电话号码
     * @return
     */
    @PostMapping("/v1/chkTel/{tel}")
    @ApiOperation(value = "账号与密码验证,获取用户的userId")
    public R checkUserTel(@PathVariable("tel") String tel){
       SysAdminUser sysAdminUser = adminUserService.getOne(new LambdaQueryWrapper<SysAdminUser>().eq(SysAdminUser::getSauPhone,tel));
       if(!ObjectUtil.isEmpty(sysAdminUser)){
            return R.suc(sysAdminUser.getSauId());
       }
        return R.error(UpmsConstant.ADMIN_USR_TEL_ERROR);
    }

    /**
     * 修改管理用户信息
     * @return
     */
    @PostMapping("/v1/editAdminUser")
    @ApiOperation(value = "修改管理用户")
    @ShunLog(title = "用户",logType = LogType.UPDATE)
    public R editAdminUser(@RequestBody @Validated SysAdminUserDto dto){
        log.debug("adminUserDto:{}",dto);
        return adminUserService.editAdminUser(dto);
    }

    /**
     * 新增管理者账号
     * @return
     */
    @PostMapping("/v1/addAdminUser")
    @ApiOperation(value = "新增管理者")
    @ShunLog(title = "用户",logType = LogType.INSERT)
    public R addAdminUser(@RequestBody @Validated SysAdminUserDto dto){
        log.debug("adminUserDto:{}",dto);
        return adminUserService.addAdminUser(dto);
    }

    /**
     * 查询管理者详细
     * @return
     */
    @PostMapping("/v1/getAdminUserInfo")
    @ApiOperation(value = "管理者详细")
    public R getAdminUserInfo(){
        SysAdminUser adminUser = adminUserService.getById(StpUtil.getLoginIdAsString());
        return (adminUser == null)? RC.FEIGN_FAIL.toR(UpmsConstant.NO_ADMIN_USER):R.suc(adminUser);
    }

    /**
     * 查询管理者详细
     * @return
     */
    @PostMapping("/v1/getAdminUser/{userId}")
    @ApiOperation(value = "管理者详细")
    public R addAdminUser(@PathVariable("userId")String userId){
        log.debug("adminUserId:{}",userId);
        SysAdminUser adminUser = adminUserService.getById(userId);
        return (adminUser == null)? RC.FEIGN_FAIL.toR(UpmsConstant.NO_ADMIN_USER):R.suc(adminUser);
    }

    /**
     * 查询管理者详细
     * @return
     */
    @GetMapping("/v1/delAdminUser/{userId}")
    @ApiOperation(value = "删除管理者")
    @ShunLog(title = "用户",logType = LogType.DELETE)
    public R delAdminUser(@PathVariable("userId")String userId){
        log.debug("adminUserId:{}",userId);
        return adminUserService.delAdminUserByUserId(userId);
    }

    /**
     * 管理者分页
     * @return
     */
    @PostMapping("/v1/pageAdminUser")
    @ApiOperation(value = "管理者分页")
    public R pageAdminUser(@RequestBody SysAdminUserQueryDto dto){
        log.debug("查询参数:{}", JSONUtil.toJsonPrettyStr(dto));
        return adminUserService.pageAdminUser(dto);
    }

    /**
     * 查询管理者详细
     * @return
     */
    @PostMapping("/v1/getInfo")
    @ApiOperation(value = "前端用户的详细")
    public R getInfo(){
        return adminUserService.getUserAndRoleAndMenus();
    }

    /**
     * 获取用户的详细信息
     * @return
     */
    @PostMapping("/v1/getDetail")
    public R getDetailInfo(){
        log.debug("获取用户详细信息！");
        SysAdminUser adminUser = adminUserService.getById(StpUtil.getLoginIdAsString());
        if(ObjectUtil.isNotEmpty(adminUser)){
            return R.suc(adminUser);
        }
        return R.error("不存在此用户！");
    }

    /**
     * 获取用户名称和ID的信息
     * @return
     */
    @PostMapping("/v1/getNameAndId")
    @ApiOperation(value = "获取用户名称和ID的信息")
    public R getNameAndId(){
        log.debug("获取角色名称和ID的列表信息！");
        return adminUserService.getUserNameAndId();
    }

    /**
     * 根据角色ID获取用户列表
     * @return
     */
    @GetMapping("/v1/getUsersByRoleId")
    @ApiOperation(value = "根据角色ID获取用户列表")
    public R getUsersByRoleId(String roleId){
        log.debug("根据角色ID获取用户列表！");
        return adminUserService.getUsersByRoleId(roleId);
    }
}